package org.example.common.bean;

import cn.hutool.json.JSONUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.example.common.enums.ErrorEnum;

import java.io.Serializable;

/**
 * 通用返回参数类.
 *
 * @author dingcan
 */
@Setter
@Getter
@Accessors(chain = true)
@NoArgsConstructor
public class ResponseBean<T> implements Serializable {
    
    private static final long serialVersionUID = -3699842403600131110L;
    /**
     * 状态码
     */
    private int code;
    
    /**
     * 消息
     */
    private String message;
    
    /**
     * 结果数据
     */
    private T data;
    
    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
    
    
    public static <T> ResponseBean<T> ok() {
        return restResult(null, ErrorEnum.SUCCESS.getErrorCode(), ErrorEnum.SUCCESS.getErrorMsg());
    }
    
    public static <T> ResponseBean<T> ok(T data) {
        return restResult(data, ErrorEnum.SUCCESS.getErrorCode(), ErrorEnum.SUCCESS.getErrorMsg());
    }
    
    public static <T> ResponseBean<T> ok(T data, String msg) {
        return restResult(data, ErrorEnum.SUCCESS.getErrorCode(), msg);
    }
    
    public static <T> ResponseBean<T> success() {
        return restResult(null, ErrorEnum.SUCCESS.getErrorCode(), ErrorEnum.SUCCESS.getErrorMsg());
    }
    
    public static <T> ResponseBean<T> success(T data) {
        return restResult(data, ErrorEnum.SUCCESS.getErrorCode(), ErrorEnum.SUCCESS.getErrorMsg());
    }
    
    public static <T> ResponseBean<T> success(T data, String message) {
        return restResult(data, ErrorEnum.SUCCESS.getErrorCode(), message);
    }
    
    
    public static <T> ResponseBean<T> failed() {
        return restResult(null, ErrorEnum.SERVER_ERROR.getErrorCode(), null);
    }
    
    public static <T> ResponseBean<T> failed(String msg) {
        return restResult(null, ErrorEnum.SERVER_ERROR.getErrorCode(), msg);
    }
    
    public static <T> ResponseBean<T> failed(T data) {
        return restResult(data, ErrorEnum.SERVER_ERROR.getErrorCode(), null);
    }
    
    public static <T> ResponseBean<T> failed(T data, String msg) {
        return restResult(data, ErrorEnum.SERVER_ERROR.getErrorCode(), msg);
    }
    
    public static <T> ResponseBean<T> error(int code, String message, T data) {
        return restResult(data, code, message);
    }
    
    public static <T> ResponseBean<T> error(int code, String message) {
        return restResult(null, code, message);
    }
    
    public static ResponseBean<String> error(String message) {
        return restResult(null, ErrorEnum.SERVER_ERROR.getErrorCode(), message);
    }
    
    
    private static <T> ResponseBean<T> restResult(T data, int code, String msg) {
        ResponseBean<T> apiResult = new ResponseBean<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }
    
    public static <T> boolean isSuccess(ResponseBean<T> response) {
        return ErrorEnum.SUCCESS.getErrorCode() == response.getCode();
    }
    
    public static <T> boolean isFailed(ResponseBean<T> response) {
        return ErrorEnum.SUCCESS.getErrorCode() != (response.getCode());
    }
}
